// 引入插件
const gulp = require("gulp"); //引入gulp
const connect = require("gulp-connect"); //搭建本地服务器
const sass = require("gulp-sass");
const sourcemaps = require("gulp-sourcemaps");
const concat = require("gulp-concat");
const uglify = require("gulp-uglify");
const rename = require("gulp-rename");
const cleanCss = require("gulp-clean-css");
const babel = require("gulp-babel");
// 创建复制html任务
gulp.task("copyHtml", done => {
    gulp.src("*.html")
        .pipe(gulp.dest("dist"))
        .pipe(connect.reload());
    done();
})
// 压缩css文件
gulp.task("cleanCss", done => {

    gulp.src("css/*.css")
        .pipe(cleanCss())
        .pipe(gulp.dest("dist/css"))
        .pipe(connect.reload());

    done();

})
// 处理js文件
gulp.task("scripts", done => {
    gulp.src("js/*.js", "!gulpfile.js")
        .pipe(babel({
            presets: ["@babel/preset-env"]
        }))

        .pipe(gulp.dest(("dist/js")))
      
        .pipe(uglify())
        .pipe(rename({
            suffix: ".min"
        }))
        .pipe(gulp.dest("dist/js"))
        .pipe(connect.reload());
    done();
})
//


//处理图片
gulp.task("images", done => {
    gulp.src("img/**/*")
        .pipe(gulp.dest("dist/img"))
        .pipe(connect.reload());

    done()
})
//建立监听
gulp.task("watch", done => {
    gulp.watch("*.html", gulp.series("copyHtml"));
    gulp.watch("css/*.css", gulp.series("cleanCss"));
    gulp.watch("*.js", gulp.series("scripts"));
    gulp.watch("img/**/*", gulp.series("images"));

    done();
})
//服务器
gulp.task("server", done => {
    connect.server({
        "root": "dist",
        "livereload": true
    })
    done();
})
gulp.task("build", gulp.parallel("copyHtml", "cleanCss", "scripts", "images" ));

// gulp.task("default",gulp.series("server","watch"));
gulp.task("default", gulp.series("server", "watch"));